using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using SubSonic;
using SubSonic.Utilities;

//Generated on 18/07/2007 15:55:02 by rupert_bates

namespace OpenProject.DataObjects{
    /// <summary>
    /// Strongly-typed collection for the Phase class.
    /// </summary>
    [Serializable]
    public partial class PhaseCollection : ActiveList<Phase, PhaseCollection> 
    {       
        public PhaseCollection() {}

    }

    /// <summary>
    /// This is an ActiveRecord class which wraps the Phase table.
    /// </summary>
    [Serializable]
    public partial class Phase : ActiveRecord<Phase>
    {
    
	    #region Default Settings
	    protected static void SetSQLProps() 
	    {
		    GetTableSchema();
	    }

	    #endregion
        #region Schema Accessor
	    public static TableSchema.Table Schema
        {
            get
            {
                if (BaseSchema == null)
                {
                    SetSQLProps();
                }

                return BaseSchema;
            }

        }

    	
        private static void GetTableSchema() 
	    {
            if(!IsSchemaInitialized)
            {
                //Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Phase", TableType.Table, DataService.GetInstance("OpenProject"));
                schema.Columns = new TableSchema.TableColumnCollection();
                schema.SchemaName = @"dbo";
                //columns
                
                TableSchema.TableColumn colvarPhaseId = new TableSchema.TableColumn(schema);
                colvarPhaseId.ColumnName = "PhaseId";
                colvarPhaseId.DataType = DbType.Int32;
                colvarPhaseId.MaxLength = 0;
                colvarPhaseId.AutoIncrement = true;
                colvarPhaseId.IsNullable = false;
                colvarPhaseId.IsPrimaryKey = true;
                colvarPhaseId.IsForeignKey = false;
                colvarPhaseId.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarPhaseId);
                
                TableSchema.TableColumn colvarTitle = new TableSchema.TableColumn(schema);
                colvarTitle.ColumnName = "Title";
                colvarTitle.DataType = DbType.String;
                colvarTitle.MaxLength = 100;
                colvarTitle.AutoIncrement = false;
                colvarTitle.IsNullable = true;
                colvarTitle.IsPrimaryKey = false;
                colvarTitle.IsForeignKey = false;
                colvarTitle.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarTitle);
                
                TableSchema.TableColumn colvarStartDate = new TableSchema.TableColumn(schema);
                colvarStartDate.ColumnName = "StartDate";
                colvarStartDate.DataType = DbType.DateTime;
                colvarStartDate.MaxLength = 0;
                colvarStartDate.AutoIncrement = false;
                colvarStartDate.IsNullable = true;
                colvarStartDate.IsPrimaryKey = false;
                colvarStartDate.IsForeignKey = false;
                colvarStartDate.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarStartDate);
                
                TableSchema.TableColumn colvarEndDate = new TableSchema.TableColumn(schema);
                colvarEndDate.ColumnName = "EndDate";
                colvarEndDate.DataType = DbType.DateTime;
                colvarEndDate.MaxLength = 0;
                colvarEndDate.AutoIncrement = false;
                colvarEndDate.IsNullable = true;
                colvarEndDate.IsPrimaryKey = false;
                colvarEndDate.IsForeignKey = false;
                colvarEndDate.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarEndDate);
                
                TableSchema.TableColumn colvarProjectId = new TableSchema.TableColumn(schema);
                colvarProjectId.ColumnName = "ProjectId";
                colvarProjectId.DataType = DbType.Int32;
                colvarProjectId.MaxLength = 0;
                colvarProjectId.AutoIncrement = false;
                colvarProjectId.IsNullable = true;
                colvarProjectId.IsPrimaryKey = false;
                colvarProjectId.IsForeignKey = true;
                colvarProjectId.IsReadOnly = false;
                
                
				colvarProjectId.ForeignKeyTableName = "Project";
                
                schema.Columns.Add(colvarProjectId);
                
                BaseSchema = schema;
                //add this schema to the provider
                //so we can query it later
                DataService.Providers["OpenProject"].AddSchema("Phase",schema);
            }

        }

        #endregion
        
        #region Query Accessor
	    public static Query CreateQuery()
	    {
		    return new Query(Schema);
	    }

	    #endregion
	    
	    #region .ctors
	    public Phase()
	    {
            SetSQLProps();
            SetDefaults();
            MarkNew();
        }

        
        public Phase(bool useDatabaseDefaults)
	    {
		    SetSQLProps();
		    if(useDatabaseDefaults)
		    {
				ForceDefaults();
			}

			MarkNew();
	    }

	    public Phase(object keyID)
	    {
		    SetSQLProps();
            SetDefaults();
		    LoadByKey(keyID);
	    }

    	 
	    public Phase(string columnName, object columnValue)
        {
            SetSQLProps();
            SetDefaults();
            LoadByParam(columnName,columnValue);
        }

        
	    #endregion
	    
	    #region Props
	    
          
        [XmlAttribute("PhaseId")]
        public int PhaseId 
	    {
		    get { return GetColumnValue<int>("PhaseId"); }

            set 
		    {
			    SetColumnValue("PhaseId", value);
            }

        }

	      
        [XmlAttribute("Title")]
        public string Title 
	    {
		    get { return GetColumnValue<string>("Title"); }

            set 
		    {
			    SetColumnValue("Title", value);
            }

        }

	      
        [XmlAttribute("StartDate")]
        public DateTime? StartDate 
	    {
		    get { return GetColumnValue<DateTime?>("StartDate"); }

            set 
		    {
			    SetColumnValue("StartDate", value);
            }

        }

	      
        [XmlAttribute("EndDate")]
        public DateTime? EndDate 
	    {
		    get { return GetColumnValue<DateTime?>("EndDate"); }

            set 
		    {
			    SetColumnValue("EndDate", value);
            }

        }

	      
        [XmlAttribute("ProjectId")]
        public int? ProjectId 
	    {
		    get { return GetColumnValue<int?>("ProjectId"); }

            set 
		    {
			    SetColumnValue("ProjectId", value);
            }

        }

	    
	    #endregion
	    
	    
	    #region PrimaryKey Methods
	    
		public OpenProject.DataObjects.WorkItemCollection WorkItemRecords
		{
			get
			{
				return new OpenProject.DataObjects.WorkItemCollection().Where(WorkItem.Columns.PhaseId, PhaseId).Load();
			}

		}

		#endregion
		
	 	
			
	    
	    
	    
	    #region ForeignKey Properties
	    
        /// <summary>
        /// Returns a Project ActiveRecord object related to this Phase
        /// 
        /// </summary>
	    public OpenProject.DataObjects.Project Project
        {
	        get { return OpenProject.DataObjects.Project.FetchByID(this.ProjectId); }

	        set
	        {
	        	SetColumnValue("ProjectId", value.ProjectId);
		        //SetColumnValue("ProjectId", value.ProjectId);
	        }

        }

	    
	    
	    #endregion
	    
	    
	    
	    //no ManyToMany tables defined (0)
	    
	    #region ObjectDataSource support
    	
    	
	    /// <summary>
	    /// Inserts a record, can be used with the Object Data Source
	    /// </summary>
	    public static void Insert(string varTitle,DateTime? varStartDate,DateTime? varEndDate,int? varProjectId)
	    {
		    Phase item = new Phase();
		    
            item.Title = varTitle;
            
            item.StartDate = varStartDate;
            
            item.EndDate = varEndDate;
            
            item.ProjectId = varProjectId;
            
	    
		    if (System.Web.HttpContext.Current != null)
			    item.Save(System.Web.HttpContext.Current.User.Identity.Name);
		    else
			    item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
	    }

    	
	    /// <summary>
	    /// Updates a record, can be used with the Object Data Source
	    /// </summary>
	    public static void Update(int varPhaseId,string varTitle,DateTime? varStartDate,DateTime? varEndDate,int? varProjectId)
	    {
		    Phase item = new Phase();
		    
                item.PhaseId = varPhaseId;
				
                item.Title = varTitle;
				
                item.StartDate = varStartDate;
				
                item.EndDate = varEndDate;
				
                item.ProjectId = varProjectId;
				
		    item.IsNew = false;
		    if (System.Web.HttpContext.Current != null)
			    item.Save(System.Web.HttpContext.Current.User.Identity.Name);
		    else
			    item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
	    }

	    #endregion
	    #region Columns Struct
	    public struct Columns
	    {
		    
		    
            public static string PhaseId = @"PhaseId";
            
            public static string Title = @"Title";
            
            public static string StartDate = @"StartDate";
            
            public static string EndDate = @"EndDate";
            
            public static string ProjectId = @"ProjectId";
            
	    }

	    #endregion
    }

}

